<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/12
 * Time: 16:10
 */
namespace App\Models;

Class Roomgag extends BaseModel
{

    protected $table = 'roomgag';
    public $timestamps = false;


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 判断用户是否禁言
     */
    public function isRoomgag($req){
          $where['uid'] = $req['uid'];
          $where['room']= $req['room'];
          $res = $this->getOne($where);
          if ($res) {
               $memberModel = new Member();
               $memberInfo = $memberModel->getOne(['id'=>$res['opid']]);
               return ['code'=>400,'msg'=>"你已被".$memberInfo['nickname'].'禁言'];
          }
        return ['code'=>200,'msg'=>"ok"];
    }
    
    /**
     * 添加用户禁言
     */
    public function addRoomgag($req){

        $room = $req['room'];
        $uid = $req['uid'];

        // 得到房间信息
        $roomModel = new Roomnum();
        $roominfo = $roomModel->getOne(['num'=>$room]);

        // 判断房管是否非法 // 判断是否是主播
        if($roominfo['uid']!=session('loginId')) {
            $where['uid'] = session('loginId');
            $where['room'] = $room;
            $roomadminmodel = new Room_admin();
            $roomadmin = $roomadminmodel->getOne($where);
            if (!$roomadmin) {
                return ['code' => 400, 'msg' => "你是非法房管"];
            }
        }
        // 判断该用户是否已经被禁言
        $bagwhere['room'] = $room;
        $bagwhere['uid']  = $uid;
        $res = $this->getOne($bagwhere);
        if ($res){
            return ['code'=>400,'msg'=>"该用户已被禁言"];
        }
        $insert['uid']   = $uid;
        $insert['room']  = $room;
        $insert['zid']   = $roominfo['uid'];
        $insert['opuid'] = session('loginId');
        $res = $this->add($insert);
        if ($res){
            return ['code'=>200,'msg'=>"禁言成功"];
        }
        return ['code'=>400,'msg'=>"禁言失败,请重试"];
    }

    /**
     * 获取禁言列表
     */
    public function gaglist(){
         $loginId = session('loginId');
         $gagdata = $this->getList(['opuid'=>$loginId]);
         if (empty($gagdata)) {
              return ['code'=>'400','msg'=>"禁言列表为空"];
         }
         $gagdata = $gagdata->toArray();
         $member = new Member();
         foreach ($gagdata as $k => $v) {
              $memberinfo = $member->getOne(['id'=>$v['uid']],'nickname');
              $gagdata[$k]['nickname'] = $memberinfo['nickname'];
         }
         return ['code'=>200,'msg'=>'ok','data'=>$gagdata];
    }

    /**
     * 取消禁言
     */
    public function delgag($req){
        $where['uid'] = $req['uid'] ;
        $where['room']= $req['room'];
        $where['opuid'] = session('loginId');
        $res = $this->getOne($where);
        if (empty($res)){
            return ['code'=>'400','msg'=>"参数错误"];
        }
        $del = $this->del($where);
        echo $del;
    }
}